package it.antonio.ilpizze.ioc.util;

import it.antonio.ilpizze.ioc.DecorationFactory;
import it.antonio.ilpizze.ioc.Dependencies;
import it.antonio.ilpizze.reflection.PropertyAccessor;

public abstract class PropertyMatcher extends Matcher<PropertyAccessor> {

	public <T> DecorationFactory<T> autowire() {
		return DecorationFactory.properties(this, new Injector<PropertyAccessor>() {

			@Override
			public void inject(PropertyAccessor prop, Object instance, Dependencies dep) {
				Object propInstance = dep.get(prop.getType());
				prop.set(instance, propInstance);
			}
		});
		
	}

	public <T> DecorationFactory<T> inject(final Object key) {
		return DecorationFactory.properties(this, new Injector<PropertyAccessor>() {

			@Override
			public void inject(PropertyAccessor prop, Object instance, Dependencies dep) {
				Object propInstance = dep.get(key);
				prop.getSetter().invoke(instance, propInstance);
			}
		});

	}
	public <T> DecorationFactory<T> inject(Injector<PropertyAccessor> injector) {
		return DecorationFactory.properties(this, injector);
	}

}
